package com.mobimagic.lockscreen.util;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.View;

import com.mobimagic.lockscreen.R;
import com.nineoldandroids.animation.ArgbEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;

public class ColorUtil {

    /**
     * progess[0,colors.length-1]从min=0 max=colors.length-1
     * 用法
     * 如果colors的大小为2，progress的范围是[0,1]，如果progress=0,返回colors[0]颜色值，progress=0.5f返回[0,1]两个颜色中间颜色值，如果progress=1返回colors[1]
     * 颜色值
     *
     * 如果colors的大小为3，progress的范围是[0,1]，如果progress=0,返回colors[0]颜色值，progress=0.5f返回[0,1]两个颜色中间颜色值，如果progress=1返回colors[0]
     * 颜色值如果如果progress=1.5f返回[1,2]两个颜色中间颜色值，如果progress=2返回colors[2]颜色值
     *
     * tanmingfeng
     *
     * @param progress
     * @param colors
     * @return
     */
    public static int getColorsValue1(float progress, int... colors) {
        int duration = 1000;
        ObjectAnimator mColorAnim = ObjectAnimator.ofInt(new Object(), "color", colors);
        mColorAnim.setEvaluator(new ArgbEvaluator());

        mColorAnim.setDuration(duration);
        long cuurentProgress = (long) (duration * progress);
        mColorAnim.setCurrentPlayTime(cuurentProgress);
        int value = (Integer) mColorAnim.getAnimatedValue();
        return value;
    }

    public static ObjectAnimator getArgbEvaluatorObjectAnimator() {
        int duration = 1000;
        ObjectAnimator mColorAnim = ObjectAnimator.ofInt(new Object(), "color", 0, 0);
        mColorAnim.setEvaluator(new ArgbEvaluator());
        mColorAnim.setDuration(duration);
        return mColorAnim;
    }

    /**
     * progess[0,colors.length-1]从min=0 max=colors.length-1
     * 用法
     * 如果colors的大小为2，progress的范围是[0,1]，如果progress=0,返回colors[0]颜色值，progress=0.5f返回[0,1]两个颜色中间颜色值，如果progress=1返回colors[1]
     * 颜色值
     *
     * 如果colors的大小为3，progress的范围是[0,1]，如果progress=0,返回colors[0]颜色值，progress=0.5f返回[0,1]两个颜色中间颜色值，如果progress=1返回colors[0]
     * 颜色值如果如果progress=1.5f返回[1,2]两个颜色中间颜色值，如果progress=2返回colors[2]颜色值
     *
     * tanmingfeng
     *
     * @param colorObj
     *            防止重复的创建对象
     * @param progress
     * @param colors
     * @return
     */

    public static int getColorsValue(ObjectAnimator colorObj, float progress, int... colors) {
        int value = 0;
        int duration = 1000;
        if (colorObj == null) {
            colorObj = ObjectAnimator.ofInt(new Object(), "color", colors);
            colorObj.setEvaluator(new ArgbEvaluator());
            colorObj.setDuration(duration);
        }
        colorObj.setIntValues(colors);
        long cuurentProgress = (long) (duration * progress);
        colorObj.setCurrentPlayTime(cuurentProgress);
        value = (Integer) colorObj.getAnimatedValue();
        return value;
    }

    /**
     * 自定义圆角按钮背景颜色设置
     *
     * @param view
     * @param color
     */
    public static void setBackgroundColor(View view, int color) {
        GradientDrawable gradientDrawable = (GradientDrawable) view.getBackground();
        gradientDrawable.mutate(); // needed line
        gradientDrawable.setStroke(1, color);
        gradientDrawable.setColor(color);
        if (Build.VERSION.SDK_INT < 11) {
            gradientDrawable.invalidateSelf();
        }
    }

}
